package elementos;

import java.io.IOException;
import java.net.URL;

import mapControl.GraphicsMapControl;

import com.jme.bounding.BoundingBox;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;
import com.jme.renderer.Renderer;
import com.jme.scene.Node;
import com.jmex.physics.DynamicPhysicsNode;
import com.model.md5.controller.MD5Controller;
import com.model.md5.importer.MD5Importer;
import com.model.md5.interfaces.IMD5Animation;
import com.model.md5.interfaces.IMD5Node;


public class Player extends Elemento {

	private IMD5Node body;
	private MD5Controller bodyController;

	public Player(String name, GraphicsMapControl mapControl) {
		super();
		node = mapControl.getPhysicsSpace().createDynamicNode();
		setName(name);

		String dir = "data/md5/player/";
		URL md5mesh = getClass().getClassLoader().getResource(
				dir + "Scene.md5mesh");
		URL animIdle = getClass().getClassLoader().getResource(
				dir + "idle.md5anim");
		URL animCaminar = getClass().getClassLoader().getResource(
				dir + "caminar.md5anim");
		URL animPlantar = getClass().getClassLoader().getResource(
				dir + "plantar.md5anim");
		URL animCall = getClass().getClassLoader().getResource(
				dir + "call.md5anim");

		MD5Importer importer = new MD5Importer();

		IMD5Animation idle = null;
		IMD5Animation caminar = null;
		IMD5Animation plantar = null;
		IMD5Animation call = null;
		try {

			importer.loadMesh(md5mesh, "ModelNode");
			body = importer.getMD5Node();

			importer.loadAnim(animIdle, "idle");
			idle = importer.getAnimation();

			importer.loadAnim(animCaminar, "caminar");
			caminar = importer.getAnimation();

			importer.loadAnim(animPlantar, "plantar");
			plantar = importer.getAnimation();

			importer.loadAnim(animCall, "call");
			call = importer.getAnimation();

		} catch (IOException e) {
			System.out
					.print("no se cargo bien algun dato del jugador, verificar la existencia y la correctitud de la ruta de los archivos .md5mesh y .md4anim");
		}

		bodyController = new MD5Controller(body);
		bodyController.addAnimation(plantar);
		bodyController.addAnimation(call);
		bodyController.addAnimation(caminar);
		bodyController.addAnimation(idle);
		bodyController.setActive(true);
		bodyController.setRepeatType(0);
		bodyController.setSpeed(3.5f);

		body.addController(bodyController);

		Node model = (Node) body;

		Quaternion rot;
		rot = model.getLocalRotation();
		rot.fromAngleAxis((float) +Math.toRadians(90), Vector3f.UNIT_Y);

		model.setLocalScale(.25f);
		model.setLocalTranslation(0, 7, 0);

		node.attachChild(model);
		node.setModelBound(new BoundingBox(new Vector3f(0, -8, 0), 4, 5, 4));
		node.generatePhysicsGeometry();

		row = 1;
		col = 1;
		node.setLocalTranslation(mapControl.getTraslationCentrada(1, 1));
		mapControl.addPlayer(this);

		((DynamicPhysicsNode) node).computeMass();

		node.setRenderQueueMode(Renderer.QUEUE_OPAQUE);
		
		
		mapControl.getRootNode().attachChild(node);
		mapControl.getRootNode().updateGeometricState(0, true);
		 
	}

	public MD5Controller getBodyController() {
		return bodyController;
	}
	
}
